<?php
    function pr($data,$handle=0){
        if( in_array($handle,[2,3]) ){
            echo '<!--';
        }
        if(is_bool($data) || is_null($data)){
            var_dump($data);
        }else {
            echo '<pre>';
            print_r($data);
            echo '</pre>';
        }
        if( in_array($handle,[2,3]) ){
            echo '-->';
        }
        if( in_array($handle,[1,3]) ){
            die;
        }
    }

    /**
     * 分数计算
     * @param  float  $score          [本题分数]
     * @param  [type] $subAnswer      [提交答案]
     * @param  string $standardAnswer [后台标准答案]
     * @param  string $filterAnswer   [匹配分析得分]
     * @return [type]                 [计算后的分数]
     */
    function getScore($score=0.01,$subAnswer=null,$standardAnswer='',$filterAnswer=''){
    	$subAnswer=strip_tags(htmlspecialchars_decode(htmlspecialchars_decode($subAnswer)));
    //	$standardAnswer=strip_tags(htmlspecialchars_decode(htmlspecialchars_decode($subAnswer)));
        //提交答案为空
        if( empty($subAnswer) ){
            return 0;
        }
        
        similar_text($subAnswer,$standardAnswer,$percent);
       
        if( $percent < 40 ){
            return 0;
        }
        if( $percent > 70 ){
        	return $score;
        }
        return sprintf('%.1f',$percent/100 * $score);

        //匹配后台标准答案
        if( (!empty($standardAnswer)) ){
            // $res2 = levenshtein('这空','fsda空3');
            similar_text($subAnswer,$standardAnswer,$percent);
            if( $percent < 40 ){
                return 0;
            }
            return sprintf('%.1f',$percent/100 * $score);
        }


        if( !is_array($filterAnswer) ){
            $filterAnswer = json_decode($filterAnswer,true);
        }
        $denyList = empty($filterAnswer['-1']) ? [] : $filterAnswer['-1'];//否定列表
        $validList = empty($filterAnswer['1']) ? [] : $filterAnswer['1'];//得分列表
        $synonymList = empty($filterAnswer['0']) ? [] : $filterAnswer['0'];//同义列表

        $replaceOne = function($one,$synonymList){
            $reList[] = $one;
            foreach ($synonymList as $list) {
                $key = false;
                $tmp = $one;
                foreach ($list as $value) {
                    $key = array_search($value,$tmp);
                    if( $key !== false ){
                        break;
                    }
                }
                if( $key !== false ){
                    foreach ($list as $newV) {
                        if( $newV != $value ){
                            $tmp[$key] = $newV;
                            $reList[] = $tmp;
                        }
                    }
                }
            }
            return $reList;
        };

        $denyListNew = [];
        foreach ($denyList as $one) {
            $res = $replaceOne($one,$synonymList);
            foreach ($res as $newOne) {
                $denyListNew[] = $newOne;
            }
        }

        //如果匹配上任一一个否定答案得0分
        foreach ($denyListNew as $one) {
            foreach ($one as $value) {
                if( strpos($subAnswer,$value) !== false ){
                    return 0;
                }
            }
        }

        //取包含比分最大值
        $matchNumArr = [];
        foreach ($validList as $one) {
            $newOne = $replaceOne($one,$synonymList);
            $matchMax = 0;
            foreach ($newOne as $v) {
                $match = 0;
                foreach ($v as $vv) {
                    if( strpos($subAnswer,$vv) === false ){
                        break;
                    }
                    $match++;
                }
                if( $match && $matchMax < $match){
                    $matchMax = $match;
                }
            }
            if( $matchMax ){
                $matchNumArr[] = [count($one),$matchMax];
            }
        }

        $maxScore = 0;
        foreach ($matchNumArr as $v) {
            $tmp = ($v[0]/$v[1])*$score;
            if( $maxScore < $tmp ){
                $maxScore = $tmp;
            }
        }
        return $maxScore;
    }